<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>二维码导入工具</title>
</head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="/static/xlsx.full.min.js"></script>
<script type="text/javascript">
        var wb;
        var rABS = false;

        function importExcel(obj) {
            if (obj.files.length == 0) {
                return;
            }
            var suffix = obj.files[0].name.split(".")[1]
            $('#_file_path').val(obj.files[0].name);
            if(suffix != 'xls' && suffix != 'xlsx'){
                layer.msg("Only support excel!");
                return;
            }

            var f = obj.files[0];
            var reader = new FileReader();
            reader.onload = function (e) {

                var costtime;
                data = e.target.result;
                if(rABS) {

                    wb = XLSX.read(btoa(fixdata(data)), {//手动转化
                        type: 'base64'
                    });
                } else {
                    var start = new Date().getTime();
                    wb = XLSX.read(data, {
                        type: 'binary'
                    });
                    costtime = new Date().getTime()-start;
                }

                var a=wb.SheetNames[0];
                var b=wb.Sheets[a];
                data=XLSX.utils.sheet_to_json(b);

                if(!data||data==""){
                    layer.closeAll('loading');
                    layer.msg("The file is empty please choose another file!");
                    return;
                }
                $("#exceltime").html(costtime);
                $("#exceljson").html(JSON.stringify(data));
            };
            if(rABS) {
                reader.readAsArrayBuffer(f);
            } else {
                reader.readAsBinaryString(f);
            }
            function fixdata(data) { //文件流转BinaryString
                var o = "",
                    l = 0,
                    w = 10240;
                for(; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
                o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
                return o;
            }
        }
</script>
<body>
    <input type="file" onchange="importExcel(this)"/>
    <p id="exceltime"></p>
    <br/>
    <p id="exceljson"></p>
</body>
</html>